<template>
  <el-form ref="clueRemarkRefForm" :model="clueRemarkQuery" label-width="110px" :rules="clueRemarkRules">
    <el-form-item label="ID：">
      <div class="detailItem">&nbsp;{{clueDetail.id}}</div>
    </el-form-item>
    <el-form-item label="负责人：">
      <div class="detailItem">&nbsp;{{clueDetail.ownerDO.name}}</div>
    </el-form-item>
    <el-form-item label="关联活动：">
      <div class="detailItem">&nbsp;{{clueDetail.activityDO.name}}</div>
    </el-form-item>
    <el-form-item label="姓名：">
      <div class="detailItem">&nbsp;{{clueDetail.fullName}}</div>
    </el-form-item>
    <el-form-item label="称呼：">
      <div class="detailItem">&nbsp;{{clueDetail.appellationDO.typeValue}}</div>
    </el-form-item>
    <el-form-item label="手机：">
      <div class="detailItem">&nbsp;{{clueDetail.phone}}</div>
    </el-form-item>
    <el-form-item label="微信：">
      <div class="detailItem">&nbsp;{{clueDetail.weixin}}</div>
    </el-form-item>
    <el-form-item label="QQ：">
      <div class="detailItem">&nbsp;{{clueDetail.qq}}</div>
    </el-form-item>
    <el-form-item label="邮箱：">
      <div class="detailItem">&nbsp;{{clueDetail.email}}</div>
    </el-form-item>
    <el-form-item label="年龄：">
      <div class="detailItem">&nbsp;{{clueDetail.age}}</div>
    </el-form-item>
    <el-form-item label="职业：">
      <div class="detailItem">&nbsp;{{clueDetail.job}}</div>
    </el-form-item>
    <el-form-item label="年收入：">
      <div class="detailItem">&nbsp;{{clueDetail.yearIncome}}</div>
    </el-form-item>
    <el-form-item label="住址：">
      <div class="detailItem">&nbsp;{{clueDetail.address}}</div>
    </el-form-item>
    <el-form-item label="是否贷款：">
      <div class="detailItem">&nbsp;{{clueDetail.needLoanDO.typeValue}}</div>
    </el-form-item>
    <el-form-item label="意向状态：">
      <div class="detailItem">&nbsp;{{clueDetail.intentionStateDO.typeValue}}</div>
    </el-form-item>
    <el-form-item label="意向产品：">
      <div class="detailItem">&nbsp;{{clueDetail.intentionProductDO.name}}</div>
    </el-form-item>
    <el-form-item label="线索状态：">
      <div class="detailItem">&nbsp;{{clueDetail.stateDO.typeValue}}</div>
    </el-form-item>
    <el-form-item label="线索来源：">
      <div class="detailItem">&nbsp;{{clueDetail.sourceDO.typeValue}}</div>
    </el-form-item>
    <el-form-item label="线索描述：">
      <div class="detailItem">&nbsp;{{clueDetail.description}}</div>
    </el-form-item>
    <el-form-item label="下次联系时间：">
      <div class="detailItem">&nbsp;{{clueDetail.nextContactTime}}</div>
    </el-form-item>
    <el-form-item label="创建时间：">
      <div class="detailItem">&nbsp;{{clueDetail.createTime}}</div>
    </el-form-item>
    <el-form-item label="创建人：">
      <div class="detailItem">&nbsp;{{clueDetail.createByDO.name}}</div>
    </el-form-item>
    <el-form-item label="编辑时间：">
      <div class="detailItem">&nbsp;{{clueDetail.editTime}}</div>
    </el-form-item>
    <el-form-item label="编辑人：">
      <div class="detailItem">&nbsp;{{clueDetail.editByDO.name}} </div>
    </el-form-item>

    <el-form-item label="填写跟踪记录：" prop="noteContent">
      <el-input
          v-model="clueRemarkQuery.noteContent"
          :rows="5"
          type="textarea"
          placeholder="请输入跟踪记录"/>
    </el-form-item>
    <el-form-item label="跟踪方式：" prop="noteWay">
      <el-select
          v-model="clueRemarkQuery.noteWay"
          placeholder="请选择跟踪方式"
          style="width: 100%"
          @click="loadDicValue('noteWay')"
          clearable>
        <el-option
            v-for="item in noteWayOptions"
            :key="item.id"
            :label="item.typeValue"
            :value="item.id"/>
      </el-select>
    </el-form-item>
  </el-form>
  <el-form-item>
  <span class="dialog-footer">
        <el-button type="primary" @click="ClueRemarkSubmit">提 交</el-button>
        <el-button type="success" @click="convertCustomer" v-if="clueDetail.state !== -1">转换客户</el-button>
        <el-button @click="goBack">关 闭</el-button>
      </span>
  </el-form-item>

  <el-form-item label="历史跟踪记录：">
    <el-table
        :data="clueRemarkList"
        style="width: 100%">
      <el-table-column type="index" label="序号" width="55"/>
      <el-table-column property="noteWayDO.typeValue" label="跟踪方式"/>
      <el-table-column property="noteContent" label="跟踪内容"/>
      <el-table-column property="createTime" label="跟踪时间"/>
      <el-table-column property="createByDO.name" label="跟踪人"/>
      <el-table-column property="editTime" label="编辑时间"/>
      <el-table-column property="editByDO.name" label="编辑人"/>
      <el-table-column label="操作" show-overflow-tooltip>
        <template #default="scope"><!--插槽-->
          <a href="javascript:" @click="edit(scope.row.id)">编辑</a>
          &nbsp;
          <a href="javascript:" @click="del(scope.row.id)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total"
        @current-change="page"
        @prev-click="page"
        @next-click="page" />

  </el-form-item>
  <!--线索转换为客户的弹窗（对话框）-->
  <el-dialog v-model="convertCustomerDialogVisible" title="线索转换客户" width="55%" center>
    <el-form ref="convertCustomerRefForm" :model="customerQuery" label-width="110px" :rules="convertCustomerRules">
      <el-form-item label="意向产品" prop="product">
        <el-select v-model="customerQuery.product" placeholder="请选择" style="width: 100%;" @click="loadProduct">
          <el-option
              v-for="item in productOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客户描述" prop="description">
        <el-input
            v-model="customerQuery.description"
            :rows="8"
            type="textarea"
            placeholder="请输入客户描述"/>
      </el-form-item>
      <el-form-item label="下次跟踪时间" prop="nextContactTime">
        <el-date-picker
            v-model="customerQuery.nextContactTime"
            type="datetime"
            style="width: 100%;"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择下次跟踪时间"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="convertCustomerDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="convertCustomerSubmit">提 交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {doGet, doPost} from "@/http/httpRequest";
import {messageTip} from "@/util/util";

export default {
  name: "ClueDetailView",
  inject:['reload'],
  data() {
    return {
      //定义转换客户的弹窗是否弹出来，默认是false不弹出来，true就弹出来
      convertCustomerDialogVisible : false,
      customerQuery:{},
      //定义线索转换为客户的验证规则
      convertCustomerRules : {
        product : [
          { required: true, message: '请选择意向产品', trigger: ['blur', 'change'] }
        ],
        description : [
          { required: true, message: '客户描述不能为空', trigger: 'blur' },
          { min: 5, max: 255, message: '客户描述长度为5-255个字符', trigger: 'blur' }
        ],
        nextContactTime : [
          { required: true, message: '请选择下次联系时间', trigger: 'blur' }
        ]
      },
      //意向产品的下拉选项
      productOptions : [{}],
      //线索详情对象，初始值是空
      clueDetail : {
        ownerDO : {},
        activityDO : {},
        appellationDO : {},
        needLoanDO : {},
        intentionStateDO : {},
        intentionProductDO : {},
        stateDO : {},
        sourceDO : {},
        createByDO : {},
        editByDO : {},

      },
      clueRemarkQuery:{},
      noteWayOptions:[{}],
      //线索跟踪记录表单提交验证规则
      clueRemarkRules : {
        noteContent : [
          { required: true, message: '请输入跟踪内容', trigger: 'blur' },
          { min: 5, max: 255, message: '跟踪内容长度为5-255个字符', trigger: 'blur' },
        ],
        noteWay : [
          { required: true, message: '请选择跟踪方式', trigger: ['blur', 'change'] },
        ]
      },
      //跟踪记录列表数据对象，初始值是空
      clueRemarkList : [{
        noteWayDO : {},
        createByDO : {},
        editByDO : {}
      }],
      //分页时每页显示多少条数据，初始值是0
      pageSize : 0,
      //分页时总共有多少条数据，初始值是0
      total : 0,

    }
  },

  mounted() {
    this.loadClueDetail();
    this.loadClueRemarkList(1);
  },

  methods : {
    //加载线索详情
    loadClueDetail() {
      let id = this.$route.params.id;
      doGet("/api/clue/detail/" + id, {}).then(resp => {
        if (resp.data.code === 200) {
          this.clueDetail = resp.data.data;
        }
      })
    },
    //加载跟踪方式
    loadDicValue(typeCode){
      doGet("/api/dic/" + typeCode, {}).then(resp => {
        if (resp.data.code === 200) {
            this.noteWayOptions = resp.data.data
        }
      })
    },
    //线索跟踪记录（提交保存）
    ClueRemarkSubmit(){
      this.$refs.clueRemarkRefForm.validate( (isValid) => {
        if (isValid) {
          doPost("/api/clue/remark", {
            clueId : this.clueDetail.id,
            noteContent : this.clueRemarkQuery.noteContent,
            noteWay : this.clueRemarkQuery.noteWay
          }).then(resp => {
            if (resp.data.code === 200) {
              messageTip("提交成功", "success");
              //刷新一下，局部刷新
              this.reload();
            } else {
              messageTip("提交失败，原因：" + resp.data.msg, "error");
            }
          })
        }
      })
    },  //获取线索跟踪记录列表数据（分页）
    loadClueRemarkList(current) {
      let id = this.$route.params.id; //params点后面的这个id名要和路由配置中的名称相同
      doGet("/api/clue/remark", {
        current : current, //前面的current是参数名，后面的current是参数值
        clueId : id
      }).then(resp => {
        if (resp.data.code === 200) {
          this.clueRemarkList = resp.data.data.list;
          this.pageSize = resp.data.data.pageSize;
          this.total = resp.data.data.total;
        }
      })
    },//分页函数
    page(value) { //value就是要查询的那一页的页码
      this.loadClueRemarkList(value);
    },
    //线索转换为客户
    convertCustomer() {
      this.convertCustomerDialogVisible = true;
    },
    //加载在售的产品
    loadProduct() {
      doGet("/api/product/sale", {}).then(resp => {
        if (resp.data.code === 200) {
          this.productOptions = resp.data.data;
        }
      })
    },
    //线索转换为客户（提交）
    convertCustomerSubmit() {
      this.$refs.convertCustomerRefForm.validate( (isValid) => {
        if (isValid) {
          doPost("/api/clue/customer", {
            clueId : this.clueDetail.id,
            product : this.customerQuery.product,
            description : this.customerQuery.description,
            nextContactTime : this.customerQuery.nextContactTime
          }).then(resp => {
            if (resp.data.code === 200) {
              //线索转换客户成功，提示一下
              messageTip("转换成功", "success");
              //刷新一下页面
              this.reload();
            } else {
              //线索转换客户失败，提示一下
              messageTip("转换失败，原因：" + resp.data.msg, "error");
            }
          })
        }
      })
    }, goBack() {
      this.$router.go(-1);
    },

  }
}
</script>

<style scoped>
.detailItem {
  background: aliceblue;
  width: 100%;
}
</style>